package com.ruibang.glass.portal.controller;

import com.alibaba.fastjson.JSON;
import com.ruibang.glass.portal.domain.ButtonReq;
import com.ruibang.glass.portal.domain.Tree;
import com.ruibang.glass.portal.entity.MenuEntity;
import com.ruibang.glass.portal.service.MenuService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
@Slf4j
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "保存菜单", response = ResultBody.class)
    @PostMapping(value = "/saveMenu")
    public ResultBody<Void> saveMenu(@RequestBody @Valid MenuEntity menu) {
        log.info("开始保存菜单：{}", JSON.toJSONString(menu));
        return menuService.saveMenu(menu);
    }

    @ApiOperation(value = "修改菜单", response = ResultBody.class)
    @PostMapping(value = "/updateMenu")
    public ResultBody<Void> updateMenu(@RequestBody @Valid MenuEntity menu) {
        log.info("开始修改菜单：{}", JSON.toJSONString(menu));
        menuService.updateById(menu);
        return ResultBody.success();
    }

    @ApiOperation(value = "删除菜单", response = ResultBody.class)
    @GetMapping(value = "/deleteMenu/{menuId}")
    public ResultBody<Void> deleteMenu(@PathVariable(value = "menuId") String menuId) {
        return menuService.deleteMenuById(menuId);
    }

    @ApiOperation(value = "根据id获取菜单", response = ResultBody.class)
    @PostMapping(value = "/getMenu")
    public ResultBody<MenuEntity> getMenu(String menuId) {
        log.info("开始根据id获取菜单：{}", menuId);
        return menuService.getMenu(menuId);
    }

    @ApiOperation(value = "获取所有菜单树", response = ResultBody.class)
    @PostMapping(value = "/getMenuList")
    public ResultBody<Tree<MenuEntity>> getMenuList() {
        return menuService.getTree();
    }

    @ApiOperation(value = "根据用户id获取菜单树", response = ResultBody.class)
    @PostMapping(value = "/getMenuByUserId")
    public ResultBody<Tree<MenuEntity>> getMenuByUserId(String userId) {
        log.info("开始根据用户id获取菜单树：{}", userId);
        return menuService.getTreeByUserId(userId);
    }

    @ApiOperation(value = "根据角色id获取菜单树", response = ResultBody.class)
    @PostMapping(value = "/getMenuByRoleId")
    public ResultBody<Tree<MenuEntity>> getMenuByRoleId(String roleId) {
        log.info("开始根据角色id获取菜单树：{}", roleId);
        return menuService.getTreeByRoleId(roleId);
    }

    @ApiOperation(value = "根据角色id和菜单id获取按钮信息", response = ResultBody.class)
    @PostMapping(value = "/getButtonInfo")
    public ResultBody<List<MenuEntity>> getMenuByRoleId(@RequestBody ButtonReq buttonReq) {
        return menuService.getButtonInfo(buttonReq);
    }

    @ApiOperation(value = "根据菜单名称获取菜单Id", response = ResultBody.class)
    @GetMapping(value = "/getMenuIdByMenuName/{menuName}")
    public ResultBody<String> getMenuIdByMenuName(@PathVariable(value = "menuName") String menuName) {
        return ResultBody.success(menuService.getMenuIdByMenuName(menuName));
    }
}
